#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
#include "touch.h"

#define MUSIC_SIZE 3

int start_play = 0;
int stop_play = 0;
extern int x_read;
extern int y_read;

void mp3()
{
	char *mp3[MUSIC_SIZE] = {"./1.mp3","./2.mp3","./3.mp3"};
	int num = -1;
	int i = 0;
	char cmd[50];
	lcd_draw_rectangel(0, 0, 800, 480, 0xffffff);//刷屏
	//显示MP3界面 根据此时的状态 显示播放按键
	lcd_draw_rectangel(0, 380, 100, 100, 0xff0000);//开始/停止播放
	lcd_draw_rectangel(100, 380, 100, 100, 0xff00);//暂停/继续播放
	lcd_draw_rectangel(200, 380, 100, 100, 0xff);//上一曲播放
	lcd_draw_rectangel(300, 380, 100, 100, 0xffff);//下一曲播放
	lcd_draw_rectangel(700, 0, 100, 100, 0xffff);//退出
	int x = -1,y = -1;
	x_read = -1;
	y_read = -1;
	while(1)
	{
		if(x!=x_read||y!=y_read&&x_read!=-1&&y_read!=-1)
		{
			x = x_read;
			y = y_read;
			x_read = -1;
			y_read = -1;
		}
		
		if(x>=0&&x<100 && y<480&&y>=380)
		{
			if(start_play == 0)
			{
				printf("start\n");
				//开始播放
				sprintf(cmd, "madplay %s &", mp3[i]);
				system(cmd);
				start_play = 1;
			}
			else
			{
				printf("tingzhi\n");
				//停止播放
				system("killall -9 madplay");
				start_play = 0;
			}
		}
		else if(x>=100&&x<200 && y<480&&y>=380)
		{
			if(start_play == 1)
			{
				if(stop_play == 0)
				{
					printf("stop\n");
					//暂停播放
					system("killall -STOP madplay &");
					stop_play = 1;	
				}
				else
				{
					printf("continue\n");
					//继续播放
					system("killall -CONT madplay &");
					stop_play = 0;
				}
			}
		}
		else if(x>=200&&x<300 && y<480&&y>=380)
		{
			//上一曲
			if(start_play == 1)
			{
				printf("prev\n");
				if(i==0)
					i = MUSIC_SIZE;
				
				i--;
				sprintf(cmd, "madplay %s &", mp3[i]);
				printf("cmd = %s", cmd);
				system("killall -9 madplay");
				system(cmd);
			}
		}
		else if(x>=300&&x<400 && y<480&&y>=380)
		{
			//下一曲
			if(start_play == 1)
			{
				printf("next\n");
				if(i==MUSIC_SIZE-1)
					i = -1;
				
				i++;
				sprintf(cmd, "madplay %s &", mp3[i]);
				printf("cmd = %s", cmd);
				system("killall -9 madplay");
				system(cmd);
			}
		}
		else if(x>=700&&x<800 && y<100&&y>=0)
		{
			break;
		}
		x = -1;
		y = -1;
	}
}